;;; this file just contains the isr stubs that are installed from 
;;; interrupts.c. The stubs just push values, call the appropriate
;;; function which is defined in interrupts.c, pop values and
;;; execute an iret

extern handle_interrupt


; this is for every ISR but those few that push an error code
%macro ISR_ZEROERR 1
	global isr_stub_%1
	
	isr_stub_%1:
	cli
	push 0
	push %1
	pusha
	mov ax, ds
	push eax
	mov ax, 0x10
	mov ds, ax
	call handle_interrupt
	ISR_COMMON_FINISH
%endmacro

; this is for IRQs. makes sure to reset pic
%macro IRQ_STUB_MASTER 1
	global isr_stub_%1
	
	isr_stub_%1:
	cli
	push 0
	push %1
	pusha
	mov ax, ds
	push eax
	mov ax, 0x10
	mov ds, ax
	call handle_interrupt
	mov al, 0x20
	out 0x20, al
	ISR_COMMON_FINISH
%endmacro

%macro IRQ_STUB_SLAVE 1
	global isr_stub_%1
	
	isr_stub_%1:
	cli
	push 0
	push %1
	pusha
	mov ax, ds
	push eax
	mov ax, 0x10
	mov ds, ax
	call handle_interrupt
	mov al, 0x20
	out 0xa0, al
	out 0x20, al
	ISR_COMMON_FINISH
%endmacro

; these push an error code
%macro ISR_CPUERR 1
	global isr_stub_%1
	
	isr_stub_%1:
	cli
	push %1
	pusha
	mov ax, ds
	push eax
	mov ax, 0x10
	mov ds, ax
	call handle_interrupt
	ISR_COMMON_FINISH
%endmacro

; common wrappup function
%macro ISR_COMMON_FINISH 0
	pop eax
	mov ds, ax
	popa
	add esp, 8
	sti
	iret
%endmacro

%assign i 0
%rep 8      ; 0-7
	ISR_ZEROERR i
	%assign i (i + 1)
%endrep
	
ISR_CPUERR 8
ISR_ZEROERR 9
ISR_CPUERR 10
ISR_CPUERR 11
ISR_CPUERR 12
ISR_CPUERR 13
ISR_CPUERR 14
	
%assign i 15 
%rep 17      ; 15 - 31
	ISR_ZEROERR i
	%assign i (i + 1)
%endrep

%assign i 32
%rep 8				; 32-39
	IRQ_STUB_MASTER i
	%assign i (i + 1)
%endrep

%assign i 40
%rep 8				; 40-47
	IRQ_STUB_SLAVE i
	%assign i (i + 1)
%endrep

[section .data]
